var timeline = new timeline();

function timeline()
{
	this.SetDateline = function(e, selectedDate)
	{
		e = window.event ? window.event : e
		e.cancelBubble = true;
		if (e.stopPropagation)
			e.stopPropagation();

  		var dir = core.GetEl(e).parentNode.tagName == 'TD';
		var cellNode = dir ? core.GetEl(e).parentNode : core.GetEl(e).parentNode.parentNode;
		var taskNode = dir ? cellNode.previousSibling : cellNode.nextSibling;
		var rowNode = cellNode.parentNode;
  		var defSpan = taskNode.colSpan;
		var cellX = e.clientX ? e.clientX : e.x;
		var shifted = false;

		calendar.Close();
		rowNode.className = 'shifting';

		document.onmouseup = function(e)
		{
   			document.onmousemove = null;
			document.onmouseup = null;

			if (!shifted)
			{
				rowNode.className = '';
				tasklists.SelectDeadline(cellNode, null, selectedDate);
			}
			else
			{
				selectedDate = new Date(selectedDate);
				var shift = dir ? taskNode.colSpan - defSpan : defSpan - taskNode.colSpan;
				core.CallApi(cellNode.id + '&v=' + parseInt(selectedDate.getTime() / 1000 - selectedDate.getTimezoneOffset() * 60 + shift * 86400), tasklists.LoadTaskListsCallback);
			}
		}

		document.onmousemove = function(e)
	  	{
	  		e = window.event ? window.event : e;
	  		var x = e.clientX ? e.clientX : e.x;

			var span = defSpan + (dir ? 1 : -1) * parseInt((x - cellX) / cellNode.clientWidth);
			var dst = span - taskNode.colSpan;
			if (span < 1 || (dst > 0 && (!cellNode.nextSibling || !cellNode.previousSibling)))
				return;

			if (dst > 0)
   				while (dst-- > 0)
      				rowNode.removeChild(dir ? cellNode.nextSibling : cellNode.previousSibling);
			else
				while (dst++ < 0)
					rowNode.insertBefore(document.createElement('TD'), dir ? cellNode.nextSibling : cellNode);

			taskNode.colSpan = span;
			shifted = true;
	  	};
	}
	
	
	this.MoveTimeline = function(e, taskId)
	{
		e = window.event ? window.event : e
		e.cancelBubble = true;
		if (e.stopPropagation)
			e.stopPropagation();

		var startX = e.clientX ? e.clientX : e.x;
		var width = 24;

		if (taskId)
		{
			document.getElementById('contents').className += taskId ? ' taskMoving' : '';
			document.getElementById('Line' + taskId).className = 'activeTask';
		}
		
	    document.onmousemove = function(e)
	  	{
	  	    if (tasklists.Loading)
	  	        return;
	  	        
	  		var x = e.clientX ? e.clientX : e.x;
			var shift = parseInt((startX - x) / width);
			if (shift)
			{
   				if (taskId)
   				{
   					core.CallApi('ShiftTask&i=' + taskId + '&v=' + (-shift * 86400), function(html)
					{
					   tasklists.LoadTaskListsCallback(html, function() { document.getElementById('Line' + taskId).className = 'activeTask'; });
					});
   				}
   				else
					tasklists.ShiftDate(shift);
				startX = x;
			}
	  	};
	  	
		document.onmouseup = function(e)
		{
   			document.getElementById('contents').className = document.getElementById('contents').className.replace(' taskMoving', '');
   			document.onmousemove = null;
			document.onmouseup = null;
		}
	}

}